package com.example.studyapp

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import com.example.studyapp.data.jsonInteraction
import com.google.gson.Gson
import org.json.JSONObject

class register : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (getSupportActionBar()!=null){//去掉标题
            getSupportActionBar()?.hide()
        };
        setContentView(R.layout.activity_register)
        val btn:Button=this.findViewById(R.id.register_btn)
        val username_reg:EditText=this.findViewById(R.id.username_reg)
        val username_sex:EditText=this.findViewById(R.id.username_sex)
        val password1_reg:EditText=this.findViewById(R.id.password1_reg)
        val password2_reg:EditText=this.findViewById(R.id.password2_reg)
        val reg_number:EditText=this.findViewById(R.id.reg_number)
        val reg_email:EditText=this.findViewById(R.id.reg_email)
        btn.setOnClickListener{
            //判断密码
            if (password1_reg.text.toString()!=password2_reg.text.toString()){
                Toast.makeText(this,"密码不匹配",Toast.LENGTH_LONG)
                        .show()
            }else{
                val js:Jstruct=myjson1.get(json_enum_dataclass.register)
                js.client.register.username=username_reg.text.toString()
                js.client.register.password=password1_reg.text.toString()
                js.client.register.number=reg_number.text.toString()
                js.client.register.email=reg_email.text.toString()
                if(username_sex.text.toString()=="女"){
                    js.client.register.sex=0
                }else if(username_sex.text.toString()=="男"){
                    js.client.register.sex=1
                }else{
                    js.client.register.sex=2
                }
                val jobj= JSONObject(Gson().toJson(js))
                object : jsonInteraction(jobj){
                    override fun myResposeListener(response: JSONObject) {
                        //处理返回信息
                        val serj:Jstruct=myjson1.tojstu(response.toString())
                        if(serj.error){//登陆失败
                            //username.setText(response.toString())
                            Toast.makeText(this@register,"注册失败，请检查网络", Toast.LENGTH_LONG)
                                    .show()
                        }else{
                            Toast.makeText(this@register,"注册成功", Toast.LENGTH_LONG)
                                    .show()
                            this@register.finish()
                        }
                    }
                }
            }

        }

    }
}